package com.ruan.controller;

import com.ruan.entity.*;
import com.ruan.query.SpuQuery;
import com.ruan.requestdata.SkuRequestData;
import com.ruan.service.*;
import com.ruan.vo.SpecVo;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName:Category
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/9/8 14:40
 **/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrKeyService attrKeyService;

    @Autowired
    private SpecKeyService specKeyService;

    @Autowired
    private SpecOptionsService specOptionsService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuService spuService;

    @Autowired
    private SpecValueService specValueService;

    @PostMapping("/addCategory")
    @PreAuthorize("hasAuthority('addCategory')")
    public R<Void> addCategory(@Valid Category category){
        return categoryService.addCategory(category);
    }

    @RequestMapping("/getCategoryList")
    @PreAuthorize("hasAuthority('getCategoryList')")
    public R<List<Category>> getCategoryList(){
        return categoryService.getCategoryList();
    }

    @RequestMapping("/getAllCategoryList")
    @PreAuthorize("hasAuthority('getAllCategoryList')")
    public R<List<Category>> getAllCategoryList(){
        return categoryService.getAllCategoryList();
    }

    @PostMapping("/addAttributeKey")
    @PreAuthorize("hasAuthority('addAttributeKey')")
    public R<Void> addAttributeKey(@Valid AttrKey attrKey){
        return attrKeyService.addAttributeKey(attrKey);
    }

    @PostMapping("/getAttributeKey")
    @PreAuthorize("hasAuthority('getAttributeKey')")
    public R<List<AttrKey>> getAttributeKey(Integer categoryId){
        return attrKeyService.getAttributeKey(categoryId);
    }

    @PostMapping("/getSpecVoByCategoryId")
    @PreAuthorize("hasAuthority('getSpecVoByCategoryId')")
    public R<List<SpecVo>> getSpecVoByCategoryId(Integer categoryId){
        return specKeyService.getSpecVoByCategoryId(categoryId);
    }

    @PostMapping("/addSpecKey")
    @PreAuthorize("hasAuthority('addSpecKey')")
    public R<Void> addSpecKey(@NotBlank String title, @NotNull Integer categoryId){
        return specKeyService.addSpecKey(title,categoryId);
    }

    @PostMapping("/addSpecOptions")
    @PreAuthorize("hasAuthority('addSpecOptions')")
    public R<Void> addSpecOptions(@NotBlank String title, @NotNull Integer specKeyId,@NotNull Integer categoryId){
        return specOptionsService.addSpecOptions(title,specKeyId,categoryId);
    }

    @PostMapping("/getSpecOptionsByKeyId")
    @PreAuthorize("hasAuthority('getSpecOptionsByKeyId')")
    public R<List<SpecOptions>> getSpecOptionsByKeyId(Integer specKeyId){
        return specOptionsService.getSpecOptionsByKeyId(specKeyId);
    }

    @DeleteMapping("/deleteSpecOptionById/{id}")
    @PreAuthorize("hasAuthority('deleteSpecOptionById')")
    public R<Void> deleteSpecOptionById(@PathVariable Integer id){
        return specOptionsService.deleteSpecOptionById(id);
    }

    @DeleteMapping("/deleteSpecKeyById/{id}")
    @PreAuthorize("hasAuthority('deleteSpecKeyById')")
    public R<Void> deleteSpecKeyById(@PathVariable Integer id){
        return specKeyService.deleteSpecKeyById(id);
    }

    @DeleteMapping("/deleteAttributeKeyById/{id}")
    @PreAuthorize("hasAuthority('deleteAttributeKeyById')")
    public R<Void> deleteAttributeKeyById(@PathVariable Integer id){
        return attrKeyService.deleteAttributeKeyById(id);
    }

    @PreAuthorize("hasAuthority('uploadSkuPicture')")
    @PostMapping("/uploadSkuPicture")
    public R<String> uploadSkuPicture(MultipartFile file) throws IOException {
        return skuService.uploadEventsPicture(file);
    }

    @PostMapping("/addSpuSku")
    @PreAuthorize("hasAuthority('addSpuSku')")
    public R<Void> addSpuSku(@RequestBody SkuRequestData skuRequestData){
        return skuService.addSpuSku(skuRequestData.getCategoryId(), skuRequestData.getAttrValueList(),skuRequestData.getSkuList(),skuRequestData.getSpu());
    }

    @RequestMapping("/getAttrKeyOptionsById/{id}")
    @PreAuthorize("hasAuthority('getAttrKeyOptionsById')")
    public R<String[]> getAttrKeyOptionsById(@PathVariable Integer id){
        return attrKeyService.getAttrKeyOptionsById(id);
    }

    @PostMapping("/deleteAttrKeyOptionByIndex")
    @PreAuthorize("hasAuthority('deleteAttrKeyOptionByIndex')")
    public R<Void> deleteAttrKeyOptionByIndex(Integer categoryId,Integer attrKeyId,Integer index){
        return attrKeyService.deleteAttrKeyOptionByIndex(categoryId,attrKeyId,index);
    }

    @PostMapping("/getSpuList")
    @PreAuthorize("hasAuthority('getSpuList')")
    public R<List<Spu>> getSpuList(SpuQuery spuQuery){
        return spuService.getSpuList(spuQuery);
    }

    @RequestMapping("/getSkuBySpuId/{spuId}")
    @PreAuthorize("hasAuthority('getSkuBySpuId')")
    public R<List<Sku>> getSkuBySpuId(@PathVariable Integer spuId){
        return skuService.getSkuBySpuId(spuId);
    }

    @RequestMapping("/getSpuById/{id}")
    @PreAuthorize("hasAuthority('getSpuById')")
    public R<Spu> getSpuById(@PathVariable Integer id){
        return spuService.getSpuById(id);
    }

    @RequestMapping("/getSpecOptionsBySpuId/{spuId}")
    @PreAuthorize("hasAuthority('getSpecOptionsBySpuId')")
    public R<List<SpecVo>> getSpecOptionsBySpuId(@PathVariable Integer spuId){
        return specValueService.getSpecOptionsBySpuId(spuId);
    }

    @RequestMapping("/editCategory")
    @PreAuthorize("hasAuthority('editCategory')")
    public R<Void> editCategory(String id,String name){
        return categoryService.editCategory(id,name);
    }

    @RequestMapping("/disableCategoryById")
    @PreAuthorize("hasAuthority('disableCategoryById')")
    public R<Void> disableCategoryById(Integer id){
        return categoryService.disableCategoryById(id);
    }

    @RequestMapping("/enableCategoryById")
    @PreAuthorize("hasAuthority('enableCategoryById')")
    public R<Void> enableCategoryById(Integer id){
        return categoryService.enableCategoryById(id);
    }

    @RequestMapping("/deleteSpuById")
    @PreAuthorize("hasAuthority('deleteSpuById')")
    public R<Void> deleteSpuById(Integer id){
        return spuService.deleteSpuById(id);
    }

    @RequestMapping("/editSku")
    @PreAuthorize("hasAuthority('editSku')")
    public R<Void> editSku(Sku sku){
        return skuService.editSku(sku);
    }

    @RequestMapping("/getAllSkuBySpuId/{spuId}")
    @PreAuthorize("hasAuthority('getAllSkuBySpuId')")
    public R<List<Sku>> getAllSkuBySpuId(@PathVariable Integer spuId){
        return skuService.getAllSkuBySpuId(spuId);
    }
}
